package cn.tedu.csmall.passport.security;

import cn.tedu.csmall.passport.mapper.AdminMapper;
import cn.tedu.csmall.passport.pojo.vo.AdminloginInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    //根据调用AdminMapper对象，根据用户名(参数值)查询管理员信息
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
         log.debug("根据用户名【{}】从数据查询用户信息.....",s);
        //调用AdminMapper的对象，根据用户名（参数值）查询管理员信息
        AdminloginInfoVO adminloginInfoVO = adminMapper.getAdminloginInfoVO(s);
       //判断是否查询到有效结果
        if(adminloginInfoVO==null){
            //根据用户名没有找到任何管理员信息
            String message="登录失败，用户名不存在！";
            log.warn(message);
            throw new UsernameNotFoundException(message);
        }
        log.debug("根据用户名【{}】从数据库查询到有效的用户信息：{}",s,adminloginInfoVO);
        //从查询结果中找出权限信息
        List<String> permissions=adminloginInfoVO.getPermission();
        List<SimpleGrantedAuthority> authorities=new ArrayList<>();
        for (String permission:permissions) {
        authorities.add(new SimpleGrantedAuthority(permission));
        }
        //返回AdminDetails类型的
        AdminDetails adminDetails=new AdminDetails(
                adminloginInfoVO.getUsername(),adminloginInfoVO.getPassword(),
                adminloginInfoVO.getEnable()==1,authorities);
        adminDetails.setId(adminloginInfoVO.getId());
        log.debug("即将向Spring Security返回UserDetails：{}",adminDetails);
        return adminDetails;
    }
}
